﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Documents;

namespace my_google_doc_online
{
    public partial class LoginForm : Form
    {
        public MyMethod m = new MyMethod();
        private PorxyForm pf = new PorxyForm();
        private string host = "";
        private int port;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void loginBtn_Click(object sender, EventArgs e)
        {
            if (Username.Text == "")
            {
                MessageBox.Show("请输入用户名", "请输入", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Password.Text == "")
            {
                MessageBox.Show("请输入密码", "请输入", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            if (host != "")
            {
                try
                {
                    
                    for (int i = 0; i < Username.Text.Length; i++)
                    {
                        if (Regex.IsMatch(Username.Text[i].ToString(), @"[\u4e00-\u9fbb]+$"))
                        {
                            MessageBox.Show("请输入正确的用户名", "登录错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            //执行MyMethod.Login方法
                            m.Login(Username.Text, Password.Text, host, port);
                        }
                    }    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("登录失败: " + ex.Message, "登录错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                try
                {
                    //Regex reg = new Regex(@"^[\u4e00-\u9fa5]+$ ");
                    for (int i = 0; i < Username.Text.Length; i++)
                    {
                        if (Regex.IsMatch(Username.Text[i].ToString(), @"[\u4e00-\u9fbb]+$"))
                        {
                            MessageBox.Show("请输入正确的用户名", "登录错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            m.Login(Username.Text, Password.Text);
                        }
                    }
                    //byte[] array = new byte[2];
                    //array = System.Text.Encoding.Unicode.GetBytes(Username.Text);
                    //int i1 = (short)(array[0] - '\0');
                    //int i2 = (short)(array[1] - '\0');

                    //bool y = reg.IsMatch("");
                    //if (y)
                    //{
                    //    MessageBox.Show("请输入正确的用户名", "登录错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //}
                    //else
                    //{
                    //    m.Login(Username.Text, Password.Text);
                    //}
                    //m.Login("lsk642", "02156688512");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("登录失败: " + ex.Message, "登录错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            DialogResult = DialogResult.OK;
        }

        private void registerBtn_Click(object sender, EventArgs e)
        {
            Process.Start(
                "https://accounts.google.com/SignUp?continue=https%3A%2F%2Faccounts.google.com%2FManageAccount&hl=zh-CN");
        }

        private void setPorxyButton_Click(object sender, EventArgs e)
        {
            DialogResult r = pf.ShowDialog();
            if (r == DialogResult.OK)
            {
                host = pf.host;
                port = pf.port;
            }
        }
    }
}